/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package technotheist.geo.map.vector;

import com.jme3.math.Vector3f;
import technotheist.geo.map.MapUV;
import technotheist.geo.map.MapUtil;
import technotheist.geo.map.VectorReadableMap;

/**
 *
 * @author Mitchel Strachan
 */
public class UnitMap implements VectorReadableMap<Vector3f> {

    private final Float3Map data;

    public UnitMap(byte detail) {
        this.data = new Float3Map(1 << detail);
        MapUtil.initUnit(data);
    }
//
//    private void init() {
//        Vector3f[] vecs = new Vector3f[9];
//        for (int i = 0; i < 9; i++) {
//            vecs[i] = new Vector3f();
//        }
//        data.set(0, 0, data.sideLength, MapUtil.NORTH());
//        data.set(1, data.sideLength, 0, MapUtil.SOUTH());
//        //
//        data.set(0, 0, 0, MapUtil.N0());
//        data.set(2, 0, 0, MapUtil.N1());
//        data.set(4, 0, 0, MapUtil.N2());
//        data.set(6, 0, 0, MapUtil.N3());
//        data.set(8, 0, 0, MapUtil.N4());
//        //
//        data.set(1, 0, 0, MapUtil.S0());
//        data.set(3, 0, 0, MapUtil.S1());
//        data.set(5, 0, 0, MapUtil.S2());
//        data.set(7, 0, 0, MapUtil.S3());
//        data.set(9, 0, 0, MapUtil.S4());
//        //
//        for (int i = 0; i < MapUtil.PANELS; i++) {
//            init(i, 0, 0, data.sideLength, vecs);
//        }
//    }
//    private static final int W = 0, SW = 1, S = 2, NW = 3, C = 4, SE = 5, N = 6, NE = 7, E = 8;
//
//    private void init(int panel, int u, int v, int len, Vector3f[] vecs) {
//        int hl = len / 2;
//        data.get(panel, u, v, vecs[W]);
//        data.get(panel, u + len, v, vecs[S]);
//        data.get(panel, u, v + len, vecs[E]);
//        data.get(panel, u + len, v + len, vecs[N]);
//        vecs[SW].set(vecs[S]).addLocal(vecs[W]).normalizeLocal();
//        vecs[SE].set(vecs[S]).addLocal(vecs[E]).normalizeLocal();
//        vecs[NW].set(vecs[N]).addLocal(vecs[W]).normalizeLocal();
//        vecs[NE].set(vecs[N]).addLocal(vecs[E]).normalizeLocal();
//        data.set(panel, u + hl, v, vecs[SW]);
//        data.set(panel, u, v + hl, vecs[NW]);
//        data.set(panel, u + len, v + hl, vecs[SE]);
//        data.set(panel, u + hl, v + len, vecs[NE]);
//    }

    @Override
    public Vector3f get(int p, int u, int v, Vector3f store) {
        return data.get(p, u, v, store);
    }

    @Override
    public Vector3f getNor(int p, float u, float v, Vector3f store) {
        return data.getNor(p, u, v, store).normalizeLocal();
    }

    @Override
    public Vector3f get(int p, float u, float v, Vector3f store) {
        return data.get(p, u, v, store).normalizeLocal();
    }

    @Override
    public Vector3f get(int p, int u, int v, int layer, Vector3f store) {
        return get(p, u, v, store);
    }

    @Override
    public Vector3f getNor(int p, float u, float v, int layer, Vector3f store) {
        return getNor(p, u, v, store);
    }

    @Override
    public Vector3f get(int p, float u, float v, int layer, Vector3f store) {
        return get(p, u, v, store);
    }

    @Override
    public Vector3f get(MapUV mp, Vector3f store) {
        return getNor(mp.panel(), mp.u, mp.v, store);
    }

    @Override
    public Vector3f get(MapUV mp, int layer, Vector3f store) {
        return getNor(mp.panel(), mp.u, mp.v, layer, store);
    }

    public Vector3f get(int index, Vector3f store) {
        int i = index * data.components;
        return store.set(data.get(i++), data.get(i++), data.get(i));
    }
}
